<?php
require_once './include/common.inc.php';

function get_product($bcode, $fid, $lid) 
{
	global $db;
	$and_str = '';
	if( ! empty($fid))
	{
		$sql = "select cat_id from p_fabric where id=".$fid;	
		$and_str = ' and fabric_cat = ';
	}
	else if( ! empty($lid))
	{
		$sql = "select cat_id from p_leather where id=".$lid;
		$and_str = ' and leather_cat = ';
	}
	if(!empty($sql))
	{
		$rs = $db->get_one($sql);
		$cat_id = $rs['cat_id'];
	}
	if(empty($cat_id)) return null;

	
	$sql = "select id,code,name,price_2 as price,upcharge from product where bcode ='".$bcode."' ".$and_str.$cat_id; 
	$rs = $db->get_one($sql);
	if(!empty($rs))
	{
		$rs['fabid'] = (int)$fid;
		$rs['leaid'] = (int)$lid;
	}
	
	return ! empty($rs) ? $rs : array();
}

$type = $_GET['t'];
if(empty($type))
{
	exit('');
}

switch($type)
{
	case 1:
		$bcode = urldecode(trim($_GET['bcode']));
		$pid = (int)$_GET['pid'];
		$fid = (int)$_GET['fid'];
		$lid = (int)$_GET['lid'];
		$fitemid = (int)$_GET['fitemid'];
		$rs = get_product($bcode, $fid, $lid);
		if(empty($rs))
		{
			$rs = $db->get_one('select id,code,name,price_2 as price,upcharge from product where id = '.$pid);
		}
		
		$fitem = $db->get_one('select upcharge from p_finish_item where id = '.$fitemid);
		if( ! empty($fitem) AND in_array($fitem['upcharge'],array('Yes','super')))
		{
			$rs['price'] += $rs['upcharge'];
		}
		
		$rs['price'] = number_format($rs['price'],2,'.',',');
		exit(json_encode($rs));
		break;
	case 2:
		$k = trim($_GET['k']);
		if(empty($k)) exit;
		$ary_k = explode('_',$k);

		$pid = (int)$ary_k[1];
		if(empty($pid)) exit;
		
		$sql =  "select bcode from product where id =".$pid; 
		$p = $db->get_one($sql);
		if(empty($p)) exit;
		
		$fitemid = (int)$_GET['fitemid'];
		$fitem = $db->get_one('select upcharge from p_finish_item where id = '.$fitemid);

		$choice = $_GET['choice'];
		$ary = explode('_',$choice);
		if(count($ary) >= 2)
		{
			$ary[0] = strtolower(trim($ary[0]));
			$ary[1] = (int)trim($ary[1]);
			$fid = $lid = 0;
			if($ary[0] == 'f')
			{
				$fid = $ary[1];
			}
			if($ary[0] == 'l')
			{
				$lid = $ary[1];
			}
		}
		$bcode = $p['bcode'];
		$rs = get_product($bcode, $fid, $lid);
		if(empty($rs))
		{
			$rs = $db->get_one('select id,code,name,price_2 as price,upcharge from product where id = '.$pid);
		}
		
		$cart = new cart();
		$data = $cart->getOne($k);
		if(empty($data)) exit;
		
		$data['id'] = $pid;
		$data['fabid'] = $fid;
		$data['leaid'] = $lid;
		$data['fitemid'] = $fitemid;
		
		if( ! empty($rs))
		{
			$data['code'] = $rs['code'];
			$data['price'] = $rs['price'];
			$data['name'] = $rs['name'];
			if(in_array($fitem['upcharge'],array('Yes','super')))
			{
				$data['price'] = $rs['price'] = $rs['price'] + $rs['upcharge'];
			}
			$rs['new_key'] = $cart->get_key($data);
		}
		
		$cart->emptyOne($k);
		$cart->addData(array($data));
		saveCart($db, $jansen_uid, $cart);
		
		$rs['price'] = number_format($rs['price'],2,'.',',');
		exit(json_encode($rs));
		break;
}
?>